using System.Drawing;
using System.Windows.Forms;
using CartoGraphic.Model2;

namespace CartoGraphic.MyTool
{

	internal class ToolPolyLine : ToolObj

	{
		public ToolPolyLine()
		{
            Cursor = Cursors.Default;
		}

		private PolyLine newPolyLine;
		private bool _drawingInProcess = false; 

		public override void OnMouseDown(DrawArea drawArea, MouseEventArgs e)
		{
			if (e.Button ==
			    MouseButtons.Right||e.Clicks==2)
			{
				_drawingInProcess = false;
				newPolyLine = null;
			}
			else
			{
				Point p = drawArea.BackTrackMouse(new Point(e.X, e.Y));
				if (drawArea.PenType ==
				    DrawingPens.PenType.Generic)
				{
					if (_drawingInProcess == false)
					{
                        newPolyLine = new PolyLine(p.X, p.Y, p.X + 1, p.Y + 1, drawArea.LineColor, drawArea.LineWidth);
						newPolyLine.EndPoint = new Point(p.X + 1, p.Y + 1);
						AddNewObject(drawArea, newPolyLine);
						_drawingInProcess = true;
					}
					else
					{
						newPolyLine.AddPoint(p);
						newPolyLine.EndPoint = p;
					}
				}
				else
				{
					if (_drawingInProcess == false)
					{
                        newPolyLine = new PolyLine(p.X, p.Y, p.X + 1, p.Y + 1);
						newPolyLine.EndPoint = new Point(p.X + 1, p.Y + 1);
						AddNewObject(drawArea, newPolyLine);
						_drawingInProcess = true;
					}
					else
					{
						newPolyLine.AddPoint(p);
						newPolyLine.EndPoint = p;
					}
				}
			}
		}

		public override void OnMouseMove(DrawArea drawArea, MouseEventArgs e)
		{
			drawArea.Cursor = Cursor;

			if (e.Button !=
			    MouseButtons.Left)
				return;

			if (newPolyLine == null)
				return; 

			Point point = drawArea.BackTrackMouse(new Point(e.X, e.Y));
			newPolyLine.MoveHandleTo(point, newPolyLine.HandleCount);
			drawArea.Refresh();
		}
	}
}